<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class AdmMenu extends Controller {
	var $data;
	
	function AdmMenu()
	{
		parent::Controller();

		$this-> db-> query('SET NAMES utf8');
		header("Content-Type: text/html; charset=UTF-8"); 

		$this->form_validation->set_message('signup', 'Введены неверные данные');
		
		$this->lang->load('main');
		$this->load->model('menu','',TRUE);
		$this->data['lang'] = $this->lang;
		//
		
		//Conf
		$q = $this->db->get('conf');
		if ($q -> num_rows() > 0) {
			foreach ($q -> result() as $row) {
				$conf[$row->var] = $row->val;
			}
		}
		$this->data['conf'] = &$conf;
		$this->data['userPerm'] = &$this->userperm;
		$this->data['status'] = &$this->session->userdata('status');
		$this->session->unset_userdata('status');
		//
	}

//ADD
	function add()
	{		
	//What we need
		$id = $this->uri->segment(4);
		
	//Perm
		if( !$this->userperm->checkPerm( $this->session->userdata('user_id'),'rw', $id) ){
			redirect('page/'.$id);
			die();
		}
				
	//Do work
		if( $this->input->post('cat') && $this->input->post('title') ){
			if( $this->menu->isExists($this->input->post('cat')) ){
				$this->menu->data = $_POST;
				$this->menu->insert_entry();
				$this->session->set_flashdata('status', 'Новый раздел: "'.$this->input->post('title').'" добавлен');
			}
			redirect('page/'.$id);
		}
		
	//Collecting
		$this->data['menu'] = $this->menu->get($id);
		if($id == '0')
			$this->menu->catInfo->id = 0;
		$this->data['catInfo'] = &$this->menu->catInfo;
	
	//View
		$this->data['type'] = 'admin/addMenu';
		$this->load->view('main',$this->data);
	}
	
//EDIT
	function edit()
	{
	//Conecting
		$this->load->model('page','',TRUE);
		
	//What we need	
		$id=$this->uri->segment(4);
		$this->page->var['id'] = $id;
		
	//Check Perm
		if( !$this->userperm->checkPerm( $this->session->userdata('user_id'),'re', '', $id) ){
			redirect('page/'.$id);
			die();
		}
		
	//Do
		if(isset($_POST['mid'])&&isset($_POST['title'])){
			if( $this->menu->isExists($this->input->post('mid')) ){
				$this->page->data = $_POST;
				$this->page->update_entry();
				$this->session->set_flashdata('status', 'Страница: "'.$this->input->post('title').'" измененна');
			}
			redirect('page/'.$_POST['mid']);
		}
		
	//Collecting
		$this->data['menu'] = $this->menu->get();
		//Page Data
		$page = $this->page->get();

		$this->data['page'] = $page;
		$config    = array('name' => 'body');
		if(isset($page[0]->body)) $config['content'] = $page[0]->body; else $config['content'] = '';
		$this->load->library('spaw', $config);
		//
		
	//View
		$this->data['type'] = 'admin/editPage';
		$this->load->view('main',$this->data);
	}

//REMOVE
	function remove()
	{
		//Conecting
		$this->load->model('menu','',TRUE);
		//What we need
		$id = $this->uri->segment(4);
		$this->menu->var['id'] = $id;
		
		//CheckPerm
		if( !$this->userperm->checkPerm( $this->session->userdata('user_id'),'rw', '', $id) ){
			die('Не достаточно прав');
		}
		
		//Page remove
		if(isset($_POST['menuId'])){
			//CheckPerm
			if( !$this->userperm->checkPerm( $this->session->userdata('user_id'),'rw', '', $_POST['menuId']) ){
				die('Не достаточно прав');
			}
			//What we need
			$id = $_POST['menuId'];
			
			//Do
			if($this->menu->remove_entry($id)){
				$this->session->set_flashdata('status', 'Раздел удалён');
			}else
				$this->session->set_flashdata('status', 'Ошибка');

			redirect();
		}

		//Collecting
		$this->data['menu'] = $this->menu->get($id);
		$this->data['catInfo'] = &$this->menu->catInfo;
		//View
		$this->data['type'] = 'admin/removeMenu';
		$this->load->view('main',$this->data);
	}
}

/* End of file main.php */
/* Location: ./system/application/controllers/main.php */
